
int partition(int *a, int l, int r) {
    int p = l;                       //隔板位置
    for (int i = l; i <= r - 1; ++i) //选a[r]作mid
        if (a[i] < a[r])             //小于mid的放在隔板之前
            swap(a[p++], a[i]);
    swap(a[p], a[r]);
    return p;
}

int random_partition(int *a, int l, int r) {
    swap(a[r], a[rand() % (r - l + 1) + l]);
    return partition(a, l, r);
}

void quick_sort(int *a, int l, int r, bool random = 1) {
    if (l >= r)
        return;
    int mid = random_partition(a, l, r);
    quick_sort(a, l, mid - 1);
    quick_sort(a, mid + 1, r);
}

int main() {
    srand(time(0));
    int a[MAX_SIZE + 5];
    for (int i = 1; i <= MAX_SIZE; ++i)
        a[i] = rand();
    quick_sort(a, 1, MAX_SIZE);
    for (int i = 1; i <= MAX_SIZE; ++i)
        cout << a[i] << " ";
}
